<?php
namespace App\Admin\Controllers\SalesReport;

use Encore\Admin\Controllers\AdminController;
use App\Importer\Inventory\CNImporter;
use App\Util\RExcel;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Maatwebsite\Excel\Facades\Excel;
use App\Exporter\Util\ArrExporter;
use Symfony\Component\HttpFoundation\File\UploadedFile;
class ManualIedUploadController extends AdminController{

    protected $title = '月报核算说明-13类型EU国家匹配';

    protected function grid()
    {
        return view('sales-report.payment-manual.ied.index');
    }

    public function upload(Request $request){
        $file = $request->file('source_file');
        if ($file->isValid()) {
            $re = $this->_import($file);
            if($re['flag']){
                return self::importResultArr($re['data']);
            }else{
                admin_toastr($re['data'],'error');
            }
        }else{
            admin_toastr('无效文件！','error');
        }
        return back()->withInput();
    }


    private static function importResultArr($re){
        $res[] = ['行','信息','信息'];
        foreach($re as $k => $v){
            if(!$v)continue;
            $res[] = [$k,$v[0],$v[1]];
        }
        return Excel::download(new ArrExporter($res),'上传结果.csv');
    }

    private function _import(UploadedFile $file){
        try{
            $data = Excel::toArray(new CNImporter(),$file);
            $data = $data[0];
            if(!$data)return ['flag' => false,'data' => '请上传包含有数据的文件！'];
            $count = 1;
            foreach($data as $row){
                $count++;
                $fulfillmentCenter = $row[0] ? trim($row[0]) : '';
                if(!$fulfillmentCenter){
                    $arr[$count] = ['无参数',''];
                    continue;
                }
                if(!array_key_exists($fulfillmentCenter, self::$_match)){
                    $arr[$count] = [$fulfillmentCenter , '未匹配到数据'];
                    continue;
                }
                $arr[$count] = [$fulfillmentCenter , self::$_match[$fulfillmentCenter]];
            }
            return ['flag' => true,'data' => $arr];
        }catch(\Exception $e){
            report($e);
            return ['flag' => false,'data' => '系统错误！'];
        }
    }

    public static $_match = [
        'BER3' => 'DE',
        'BRE4' => 'DE',
        'CGN1' => 'DE',
        'DTM1' => 'DE',
        'DTM2' => 'DE',
        'DUS2' => 'DE',
        'DUS4' => 'DE',
        'FRA7' => 'DE',
        'HAM2' => 'DE',
        'KTW1' => 'DE',
        'KTW3' => 'DE',
        'LCJ2' => 'DE',
        'LCJ3' => 'DE',
        'LEJ1' => 'DE',
        'LEJ3' => 'DE',
        'LEJ5' => 'DE',
        'MUC3' => 'DE',
        'PAD1' => 'DE',
        'POZ1' => 'DE',
        'POZ2' => 'DE',
        'PRG2' => 'DE',
        'STR1' => 'DE',
        'SZZ1' => 'DE',
        'WRO1' => 'DE',
        'WRO2' => 'DE',
        'WRO5' => 'DE',
        'XDEQ' => 'DE',
        'XPLA' => 'DE',
        'XPLB' => 'DE',
        'XPLD' => 'DE',
        'XPO1' => 'DE',
        'LCJ4' => 'DE',
        'FRA3' => 'DE',
        'XDEY' => 'DE',
        'XPLC' => 'DE',
        'BTS2' => 'DE',
        'BCN1' => 'ES',
        'BCN2' => 'ES',
        'MAD4' => 'ES',
        'MAD6' => 'ES',
        'MAD7' => 'ES',
        'RMU1' => 'ES',
        'SVQ1' => 'ES',
        'DUS4' => 'FR',
        'CDG7' => 'FR',
        'ETZ2' => 'FR',
        'LIL1' => 'FR',
        'LYS1' => 'FR',
        'MRS1' => 'FR',
        'ORY1' => 'FR',
        'ORY4' => 'FR',
        'XOR1' => 'FR',
        'BVA1' => 'FR',
        'BHX1' => 'GB',
        'BHX4' => 'GB',
        'BHX7' => 'GB',
        'BRS1' => 'GB',
        'BRS2' => 'GB',
        'CWL1' => 'GB',
        'EDI4' => 'GB',
        'EMA1' => 'GB',
        'EMA2' => 'GB',
        'EMA3' => 'GB',
        'EUK5' => 'GB',
        'GLA1' => 'GB',
        'LBA2' => 'GB',
        'LBA4' => 'GB',
        'LCY2' => 'GB',
        'LCY3' => 'GB',
        'LTN1' => 'GB',
        'LTN2' => 'GB',
        'LTN4' => 'GB',
        'MAN1' => 'GB',
        'MAN2' => 'GB',
        'MAN3' => 'GB',
        'MAN4' => 'GB',
        'MME1' => 'GB',
        'MME2' => 'GB',
        'NCL1' => 'GB',
        'BHX2' => 'GB',
        'LBA1' => 'GB',
        'XLT2' => 'GB',
        'BHX1' => 'IT',
        'BGY1' => 'IT',
        'BLQ1' => 'IT',
        'FCO1' => 'IT',
        'FCO2' => 'IT',
        'MXP3' => 'IT',
        'MXP5' => 'IT',
        'MXP6' => 'IT',
        'TRN1' => 'IT',
    ];

    public function template(Request $request){
        $temp = $request->get('temp');
        $suffix = $request->get('suffix','xlsx');
        return Storage::disk('admin')->download("template/{$temp}-template.{$suffix}");
    }
}
